package com.liuyk.commonlib.network;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;

import com.liuyk.commonlib.utility.FileUtils;

import java.io.File;
import java.io.RandomAccessFile;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 下载
 * <p>
 * Created by liuyakui on 2020-02-07.
 */
public class DownloadManager {
    private final ConcurrentHashMap<String, DownloadInfo> mDownloadProgress;

    private DownloadManager() {
        mDownloadProgress = new ConcurrentHashMap<>();
    }

    public static DownloadManager getInstance() {
        return Inner.INSTANCE;
    }

    public void download(final String url, final DownloadCallBack callBack) {
        download(url, "", callBack);
    }

    public void download(final String url, boolean breakpoint, final DownloadCallBack callBack) {
        download(url, "", breakpoint, callBack);
    }

    public void download(final String url, String fileName, final DownloadCallBack callBack) {
        RealCall.getInstance().download(url, getDefaultFile(fileName), false, callBack);
    }

    public void download(final String url, String fileName, boolean breakpoint, final DownloadCallBack callBack) {
        RealCall.getInstance().download(url, getDefaultFile(fileName), breakpoint, callBack);
    }

    public void download(final String url, File file, final DownloadCallBack callBack) {
        RealCall.getInstance().download(url, file, false, callBack);
    }

    public void download(final String url, boolean breakpoint, File file, final DownloadCallBack callBack) {
        RealCall.getInstance().download(url, file, breakpoint, callBack);
    }

    private File getDefaultFile(String fileName) {
        if (TextUtils.isEmpty(fileName)) {
            fileName = String.valueOf(System.currentTimeMillis());
        }
        return new File(FileUtils.getFileDir(), fileName);
    }

    public RandomAccessFile createRWDFile(File file) {
        try {
            return new RandomAccessFile(file, "rwd");
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public long getProgress(String url) {
        DownloadInfo downloadInfo = mDownloadProgress.get(url);
        if (downloadInfo == null) {
            return 0;
        }
        return downloadInfo.progress;
    }

    public long getLength(String url) {
        DownloadInfo downloadInfo = mDownloadProgress.get(url);
        if (downloadInfo == null) {
            return 0;
        }
        return downloadInfo.l;
    }

    public void setProgress(String url, long l, long progress) {
        DownloadInfo downloadInfo = mDownloadProgress.get(url);
        if (downloadInfo == null) {
            downloadInfo = new DownloadInfo(progress, l);
        }
        if (downloadInfo.l == 0) {
            downloadInfo.l = l;
        }
        downloadInfo.progress = progress;
        mDownloadProgress.put(url, downloadInfo);
    }

    public void pause(String url) {
        RealCall.getInstance().cancel(url);
    }

    public void removeProgress(String url) {
        mDownloadProgress.remove(url);
    }

    public void startDownloadServer(Activity activity, String fileUrl, String fileName) {
        final Intent intent = new Intent(activity, DownloadServer.class);
        intent.putExtra("fileUrl", fileUrl);
        intent.putExtra("fileName", fileName);
        activity.startService(intent);
    }

    private static class Inner {
        private static final DownloadManager INSTANCE = new DownloadManager();
    }
}
